package com.qf.oop.hero;

import java.util.Random;

/**
 * 武器工具类
 */
public class WeaponUtils {

    public static void main(String[] args) {
        Weapon[] weaponArr = createWeapon(5);
//        for (int i = 0; i < weaponArr.length; i++) {
//            weaponArr[i].printInfo();
//        }

        LongSword[] longSwords = new LongSword[weaponArr.length];
        Dagger[] daggers = new Dagger[weaponArr.length];
        classify(weaponArr,longSwords,daggers);
        for (int i = 0; i < longSwords.length; i++) {
            if (longSwords[i]!=null){
                longSwords[i].printInfo();
            }
        }
        System.out.println("=================");
        for (int i = 0; i < daggers.length; i++) {
            if (daggers[i]!=null){
                daggers[i].printInfo();
            }
        }
    }

    /**
     * 对weapons进行分类，把长剑和匕首分别放入对应的数组中
     * @param weapons
     * @param swords
     * @param daggers
     */
    public static void classify(Weapon[] weapons,LongSword[] swords,Dagger[] daggers){

        for (int i = 0; i < weapons.length; i++) {

            Weapon weapon = weapons[i];
            if (weapon instanceof LongSword){
                swords[i]= (LongSword) weapon;
            }else {
                daggers[i] = (Dagger) weapon;
            }

        }

    }



    /**
     * 创建指定数量的随机武器，返回武器数组
     * @param number
     * @return
     */
    public static Weapon[] createWeapon(int number){

        Weapon[] weapons = new Weapon[number];

        for (int i = 0; i < number; i++) {
            // 调用方法生成一个随机武器
            Weapon weapon = createRandomWeapon();
            weapons[i] = weapon;
        }

        return weapons;

    }

    /**
     * 私有的方法，在该类之外无法被直接使用
     * @return
     */
    private static Weapon createRandomWeapon() {

        Random random = new Random();
        int i = random.nextInt(2);

        if (i==0){
            // 创建长剑武器
            LongSword longSword = new LongSword();
            longSword.setName("新手剑");
            longSword.setAtk(10+random.nextInt(20)); // 长剑的攻击力随机
            longSword.setType(0);
            return longSword;
        }else {
            // 创建匕首
            Dagger dagger = new Dagger();
            dagger.setName("新手匕首");
            dagger.setAtk(5+random.nextInt(30));
            dagger.setType(0);
            return dagger;
        }

    }

}
